package com.liuchuanzheng.lcz_wanandroid.module.home.viewmodel

import androidx.lifecycle.MutableLiveData
import com.jeremyliao.liveeventbus.LiveEventBus
import com.kingja.loadsir.callback.Callback
import com.kingja.loadsir.callback.SuccessCallback
import com.liuchuanzheng.lcz_wanandroid.R
import com.liuchuanzheng.lcz_wanandroid.base.BaseViewModel
import com.liuchuanzheng.lcz_wanandroid.ext.showToast
import com.liuchuanzheng.lcz_wanandroid.module.common.bus.CollectStatusEvent
import com.liuchuanzheng.lcz_wanandroid.module.common.callback.ErrorCallback
import com.liuchuanzheng.lcz_wanandroid.module.home.bean.Article
import com.liuchuanzheng.lcz_wanandroid.module.home.repository.CollectRepository
import com.liuchuanzheng.lcz_wanandroid.module.home.repository.ProjectsRepository
import com.liuchuanzheng.lcz_wanandroid.net.base.PageBean

/**
 * @author 刘传政
 * @date 2021/9/14 0014 18:20
 * QQ:1052374416
 * 电话:18501231486
 * 作用:
 * 注意事项:
 */
class LatestProjectsViewModel : BaseViewModel() {
    val projectList: MutableLiveData<ResultPageBean> = MutableLiveData()
    val smartRefreshStatus: MutableLiveData<Boolean> = MutableLiveData() //下拉刷新控件的状态
    val smartLoaMoreStatus: MutableLiveData<Boolean> = MutableLiveData() //上拉加载控件的状态
    val liveData_loadServiceStatus: MutableLiveData<Class<out Callback>> = MutableLiveData() //loadsir的状态
    val liveData_loading = MutableLiveData<Boolean>()
    private val projectsRepository by lazy { ProjectsRepository() }
    private val collectRepository by lazy { CollectRepository() }
    var pageBean: PageBean = PageBean()
    fun getLatestProjects(isRefresh: Boolean) {
        launch(
            workBlock = {
                val requestPage = if (isRefresh) pageBean.serverFirstPage else pageBean.currentPage + 1
                var result = projectsRepository.getLatestProjects(requestPage)
                handleResult(result,
                    successBlock = {
                        if (isRefresh) {
                            pageBean.resetDefault()
                        }
                        var resultPageBean = ResultPageBean()
                        resultPageBean.dataList = result.data!!.datas
                        resultPageBean.isRefresh = isRefresh
                        projectList.value = resultPageBean
                        //让分页bean加一页。这必须是网络有结果后判断后再进行加一页。有的人请求时就加一页了，你想想对吗？加载更多失败了，再来一次加载更多，那不就又加一页？
                        pageBean.addCurrentPage()
                    },
                    failBlock = {
                        showToast("获取失败")
                    }
                )
                liveData_loadServiceStatus.value = SuccessCallback::class.java //页面根据自己的情况去显示success或empty
                smartRefreshStatus.value = false
                smartLoaMoreStatus.value = false

            },
            errorBlock = {
                showToast(R.string.network_not_available)
                liveData_loadServiceStatus.value = ErrorCallback::class.java
            })
    }
    fun collect(id: Long) {
        liveData_loading.value = true
        launch(
            workBlock = {
                var result = collectRepository.collect(id)
                handleResult(result,
                    successBlock = {
                        showToast("收藏成功")
                        val event = CollectStatusEvent(id,true)
                        LiveEventBus.get(CollectStatusEvent::class.java).post(event)
                    },
                    failBlock = {
                        showToast("${result.errorMsg}")
                    }
                )
            }, errorBlock = {
                showToast(R.string.network_not_available)
            }, finillyBlock = {
                liveData_loading.value = false
            }
        )
    }

    fun uncollect(id: Long) {
        liveData_loading.value = true
        launch(
            workBlock = {
                var result = collectRepository.uncollect(id)
                handleResult(result,
                    successBlock = {
                        showToast("已取消收藏")
                        val event = CollectStatusEvent(id,false)
                        LiveEventBus.get(CollectStatusEvent::class.java).post(event)
                    },
                    failBlock = {
                        showToast("${result.errorMsg}")
                    }
                )
            }, errorBlock = {
                showToast(R.string.network_not_available)
            }, finillyBlock = {
                liveData_loading.value = false
            }
        )
    }
    /**
     * 这是为了结果能带一些透传参数，view方便有不同的显示效果
     * 比如刷新操作异常，如果本来有数据，那就没必要显示空白页;而本来没数据就显示空白页
     * 所以我是反对网络接口的操作进行统一处理的，比如出错就显示toast。有的接口本来就是不同view条件下，有不同的错误提示。否则效果会让用户很反感。
     * @property page Int
     * @property dataList MutableList<Article>?
     */
    class ResultPageBean {
        var isRefresh: Boolean = true
        var dataList: MutableList<Article>? = null
    }

}